package br.unifor.cadeventos.dao;

import java.sql.Connection;
import java.sql.SQLException;

import com.jolbox.bonecp.BoneCP;
import com.jolbox.bonecp.BoneCPConfig;

public class ConnectionFactory {

	private static BoneCP managerPool = null;
	@SuppressWarnings("unused")
	private static final ConnectionFactory factory = new ConnectionFactory();
	
	private ConnectionFactory () {
		try {
			BoneCPConfig config = new BoneCPConfig();
			config.setJdbcUrl("jdbc:postgresql://localhost:5432/eventos");
			config.setUser("postgres");
			config.setPassword("unifor");
			config.setMinConnectionsPerPartition(3);
			config.setMaxConnectionsPerPartition(50);
			config.setPartitionCount(1);
			
			managerPool  = new BoneCP(config);
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println();
		}
	}

	public static Connection getConnection() throws SQLException {
		Connection conn = managerPool.getConnection();
		return conn;
	}
	
	public static void close(Connection conn) throws SQLException{
		conn.close();
	}
	
	public static void shutdown() throws SQLException{
		managerPool.shutdown();
	}
	
}
